//
//  TypeModel.m
//  LiWuShuo
//
//  Created by 袁小航 on 14/11/14.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TypeModel.h"
#import "AFAppDotNetAPIClient.h"

@implementation TypeModel

- (instancetype)initWithTopicAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    self.typeTopicImageUrl = [attributes objectForKey:@"banner_image_url"];
    self.sId = [[attributes objectForKey:@"id"] integerValue];
    self.typeTitle = [attributes objectForKey:@"title"];
    return self;
}
+ (NSURLSessionDataTask *)typeTopicWithBlock:(void (^)(NSArray *posts, NSError *error))block
{
    return [[AFAppDotNetAPIClient sharedClient] GET:@"http://api.liwushuo.com/v1/collections?limit=6&offset=0" parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSArray *postsFromResponse = [[JSON objectForKey:@"data"] objectForKey:@"collections"];
        
        NSMutableArray * mutablePosts = [NSMutableArray array];
        for (NSDictionary *attributes in postsFromResponse) {
            TypeModel *model = [[TypeModel alloc]initWithTopicAttributes:attributes];
            [mutablePosts addObject:model];
        }
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}

#pragma mark - 获取“对象”和“兴趣”的数据
- (instancetype)initWithPHAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    self.icon_url = [attributes objectForKey:@"icon_url"];
    self.name = [attributes objectForKey:@"name"];
    self.sId = [[attributes objectForKey:@"id"]integerValue];
    
    return self;
}
+ (NSURLSessionDataTask *)typePHWithBlock:(void (^)(NSArray *posts, NSError *error))block
{
    return [[AFAppDotNetAPIClient sharedClient] GET:@"http://api.liwushuo.com/v1/channel_groups/all" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *postsFromRespones1 = [[[responseObject objectForKey:@"data"] objectForKey:@"channel_groups"][0] objectForKey:@"channels"];
        NSMutableArray *mutablePosts1 = [NSMutableArray array];
        for (NSDictionary *dic1 in postsFromRespones1) {
            TypeModel *model1 = [[TypeModel alloc]initWithPHAttributes:dic1];
            [mutablePosts1 addObject:model1];
        }
        
        NSArray *postsFromRespones2 = [[[responseObject objectForKey:@"data"] objectForKey:@"channel_groups"][1]objectForKey:@"channels"];
        NSMutableArray *mutablePosts2 = [NSMutableArray array];
        for (NSDictionary *dic2 in postsFromRespones2) {
            TypeModel *model2 = [[TypeModel alloc]initWithPHAttributes:dic2];
            [mutablePosts2 addObject:model2];
        }
        NSArray *arr = @[mutablePosts1, mutablePosts2];
        
        if (block) {
            block([NSArray arrayWithArray:arr], nil);
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}


@end
